﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SiGeSIB.GUI.Common;
using SiGeSIB.Data.Finances;
using SiGeSIB.Services;
using SiGeSIB.GUI.Common.DedaGrid.Utils;
using SiGeSIB.GUI.Common.DedaGrid.Filter;

namespace SiGeSIB.GUI.Finances
{
#if DESIGN
	public partial class FManageCostCenters : FormRedirector.FManageCostCenters
#else
	public partial class FManageCostCenters : FGenericManager<CostCenter>
#endif
	{
		#region Constructors
		public FManageCostCenters()
		{
			InitializeComponent();
		} 
		#endregion
		
		#region Column Names
		private const string DT_NAME = "Nome";
		private const string DT_DESCRIPTION = "Descrição";
		private const string DT_RESPONSABLE_NAME = "Nome do responsável"; 
		#endregion

		#region Properties
		protected override string _Description
		{
			get { return "os Centros de Custos"; }
		}
		#endregion

		#region Event Handlers
		protected override void uManageDefaultToolStrip_AddClicked(object sender, EventArgs e)
		{
			_Manager.CreateParentTab(this, new FEditCostCenter(EFormState.Edit, -1));
		}

		protected override void uManageDefaultToolStrip_EditClicked(object sender, EventArgs e)
		{
			DataRow row = uEntitiesGrid.GetSelectedRow();
			if (row != null)
			{
				int currentId = (int)row[DT_ID];
				_Manager.CreateParentTab(this, new FEditCostCenter(EFormState.View, currentId));
			}
		}
		#endregion

		#region Methods
		#region Loading
		protected override void _InitializeDataColumns()
		{
			this._DedaColumns = new List<DedaGridColumn>()
			{
				new DedaGridColumn(DT_ID, typeof(int)) { Visible = false },
				new DedaGridColumn(DT_NAME, typeof(string)),
				new DedaGridColumn(DT_DESCRIPTION, typeof(string)),
				new DedaGridColumn(DT_RESPONSABLE_NAME, typeof(string))
			};
		}

		protected override object[] _ConvertToDataRow(CostCenter obj)
		{
			return new object[]
				{
					obj.Id,
					obj.Name,
					obj.Description,
					obj.Responsable != null ? obj.Responsable.Name : (object)DBNull.Value
				};
		}

		protected override List<CostCenter> _LoadEntity()
		{
			IFinancesServicesProvider servProv = _Manager.ServicesProvider.CreateFinancesServicesProvider();
			return servProv.GetCostCenters(CostCenterInfo.SpecificData);
		} 
		#endregion

		protected override bool _RequestRemove()
		{
			return MessageBox.Show(this, "Deseja remover o centro de custo selecionado?", "Confirmação", MessageBoxButtons.OKCancel) == DialogResult.OK;
		}

		protected override void _ApplyFilter(string p)
		{
			DedaGridFilterCollection collection = new DedaGridFilterCollection();
			collection.Add(new DedaGrigFilterCondition(_DtTable.Columns[DT_NAME], ELogicalCondition.CONTAINS, p));
			collection.Add(new DedaGrigFilterCondition(EDedaGridFilterLogicalConnector.OR, _DtTable.Columns[DT_DESCRIPTION], ELogicalCondition.CONTAINS, p));
			collection.Add(new DedaGrigFilterCondition(EDedaGridFilterLogicalConnector.OR, _DtTable.Columns[DT_RESPONSABLE_NAME], ELogicalCondition.CONTAINS, p));

			_DtTable.Filter(collection);
		}

		protected override void _RemoveEntity(int id)
		{
			IFinancesServicesProvider servProv = _Manager.ServicesProvider.CreateFinancesServicesProvider();
			servProv.RemoveCostCenter(id);
		} 
		#endregion
	}
}
